package shiyongapi;

import com.google.common.base.Splitter;
import org.junit.Test;

import java.util.Arrays;
import java.util.Map;

/**
 * @Auther lujieni
 * @Date 2020-11-24 10:40
 */
public class UseSplit {

    /**
     * 用"|"进行分割字符串必须使用"\\|的写法"
     */
    @Test
    public void test01(){
        String[] result = "2|33|4".split("\\|");
        System.out.println(Arrays.toString(result));
    }

    @Test
    public void test11(){
        Splitter.on("|").split("2|33|4").forEach(e-> System.out.println(e));
    }




    /**
     * 用"."进行分割字符串必须使用"\\.的写法"
     */
    @Test
    public void test02(){
        String[] result = "172.0.0.1".split("\\.");
        System.out.println(Arrays.toString(result));
    }


    @Test
    public void test12(){
        Splitter.on(".").split("127.0.0.1").forEach(e-> System.out.println(e));
    }


    /**
     * 用","进行分割字符串使用,即可
     */
    @Test
    public void test03(){
        String[] result = "172,0,0,1".split(",");
        System.out.println(Arrays.toString(result));
    }


    /**
     *
     * trimResults() 去除空格
     *
     */
    @Test
    public void test13(){
        Splitter.on(",").trimResults().omitEmptyStrings().split("a,   b, c,    ,   d,").forEach(e->{
            System.out.println(e);
        });
    }


    /**
     * @@ 进行分割后就是空字符串:""
     */
    @Test
    public void test04(){
        String content = "12@54@@h1";
        String[] result = content.split("@");
        System.out.println(result[2].length());
        System.out.println(Arrays.toString(result));
    }

    @Test
    public void test14(){
        String web = "http://www.baidu.com?age=26&name=zhangsan";
        String[] split = web.split("\\?");

        Map<String, String> map = Splitter.on("&").withKeyValueSeparator("=").split(split[1]);
        System.out.println(map);
    }


    public static void main(String[] args) {
        hello(null);
    }

    private static void hello(String name){

        for (int i = 0; i < 5; i++) {
            if(name == null){
                name = "name";
            }

        }

    }

}